package com.wutong.eams.service;

import com.wutong.eams.dto.UserManagerDTO;
import com.wutong.eams.entity.User;
import com.wutong.eams.exception.DatabaseException;
import com.wutong.eams.mapper.UserMapper;
import com.wutong.eams.other.ConditionBuilder;
import com.wutong.eams.utils.ObjectUtil;
import com.wutong.eams.utils.StringUtil;

import java.util.List;

public class UserManagerService implements IService<UserMapper, User> {

    private final UserMapper userMapper = new UserMapper() {
    };

    @Override
    public UserMapper getMapper() {
        return userMapper;
    }

    /**
     * 更新用户，当字段为null时，不更新
     *
     * @param updateUserDTO 用户对象
     */
    public boolean updateUser(UserManagerDTO.UpdateUser updateUserDTO) {
        User user = updateUserDTO.getUser();
        User oldUser = getMapper().selectByUserId(user.getUserId());
        if (oldUser == null) {
            throw new DatabaseException.QueryException("用户不存在");
        }
        ObjectUtil.copyProperties(oldUser, user, true);
        return update(user, new ConditionBuilder().where("userId", ConditionBuilder.Operator.EQUAL, user.getUserId())) > 0;
    }

    /**
     * 更新用户密码
     *
     * @param updateUserPasswordDTO 用户密码对象
     * @return 是否成功
     */
    public boolean updateUserPassword(UserManagerDTO.UpdateUserPassword updateUserPasswordDTO) {
        // 先判断旧密码是否正确
        User user = getMapper().selectByUserId(updateUserPasswordDTO.getUserId());
        if (user == null) {
            throw new DatabaseException.QueryException("用户不存在");
        }
        if (!user.getPassword().equals(updateUserPasswordDTO.getOldPassword())) {
            throw new DatabaseException.UpdateException("旧密码错误");
        }
        // 更新密码
        user.setPassword(updateUserPasswordDTO.getNewPassword());
        return updateUser(new UserManagerDTO.UpdateUser(user));
    }

    /**
     * 创建用户
     *
     * @param createUserDTO 用户对象
     * @return 是否成功
     */
    public boolean createUser(UserManagerDTO.CreateUser createUserDTO) {
        User user = createUserDTO.getUser();
        user.setPassword(StringUtil.encrypt(user.getPassword()));
        return insert(user) > 0;
    }

    /**
     * 删除用户
     *
     * @param deleteUserDTO 用户对象
     * @return 是否成功
     */
    public boolean deleteUser(UserManagerDTO.DeleteUser deleteUserDTO) {
        return delete(new ConditionBuilder().where("userId", ConditionBuilder.Operator.EQUAL, deleteUserDTO.getUserId())) > 0;
    }

    /**
     * 分页查询用户
     *
     * @param queryUserByPageDTO 分页对象
     * @return 用户列表
     */
    public List<User> queryUserByPage(UserManagerDTO.QueryUserByPage queryUserByPageDTO) {
        return selectPage(new ConditionBuilder(), queryUserByPageDTO.getPage(), queryUserByPageDTO.getSize()).getResult();
    }

    /**
     * 根据id查询用户
     *
     * @param getUserByIdDTO 用户id对象
     * @return 用户对象
     */
    public User getUserById(UserManagerDTO.GetUserById getUserByIdDTO) {
        return selectOne(new ConditionBuilder().where("userId", ConditionBuilder.Operator.EQUAL, getUserByIdDTO.getUserId()));
    }

    public int countUsers() {
        return count(new ConditionBuilder());
    }
}
